-- ABS 周卡

local Config = require("Config")
local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local AbsActDefine = require("absAct.AbsActDefine")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local MangHeLogic = require("absAct.MangHeLogic")
local MailExcel = require("excel.mail")
local MailManager = require("mail.MailManager")
local CommonDB = require("common.CommonDB")
local AbsActLogic = require("absAct.AbsActLogic")
local HeroGrid = require("hero.HeroGrid")
local DrawCardLogic = require("drawCard.DrawCardLogic")
local Log = require("common.Log")
local HeroBook = require("hero.HeroBook")
local HeroExcel = require("excel.hero")
local HeroLogic = require("hero.HeroLogic")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local Grid = require("bag.Grid")
local BuyLogic = require("topup.BuyLogic")
local BuyExcel = require("excel.buy")
local TopupLogic = require("topup.TopupLogic")
local BagLogic = require("bag.BagLogic")
local YunYingLogic = require("yunying.YunYingLogic")

DAY_CNT = 365

local function initDB(human, id)
    if not human.db.absAct[id] then return end
    human.db.absAct[id].dayCnt = human.db.absAct[id].dayCnt or DAY_CNT
    human.db.absAct[id].item = human.db.absAct[id].item or { }
    human.db.absAct[id].draw = human.db.absAct[id].draw or 1
end

function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end
    local absAct = human.db.absAct[funcConfig.funcID]
    if absAct and absAct.dayCnt and absAct.dayCnt <= 0 and #absAct.item <= 0 then
        return false
    end
    return true, endTime, startTime
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isRed(human, YYInfo, funcConfig)
    local absAct = human.db.absAct[funcConfig.funcID]
    if not absAct then return true end

    if not absAct.dayCnt or absAct.dayCnt <= 0 then return end

    if absAct.draw == 1 then
       return true 
    end

    if absAct.active then
       if #absAct.item > 0 then
          return true
       end
    end

    return false
end

function getAndSendMsg(human, id, config)
    local msgRet = Msg.gc.GC_ABS_365CARD_QUERY
    msgRet.leftTime = 0
    msgRet.actId = id

    local actId = config.actId
    local abs365CardConfig = AbsActExcel.abs365Card[actId]
    if not abs365CardConfig then return end

    local buyItemConfig = BuyExcel.libao[TopupLogic.TOPUP_LIBAO_ID3]
    if not buyItemConfig then return end

    msgRet.items[0] = #abs365CardConfig.item
    for i = 1, #abs365CardConfig.item do
        Grid.makeItem(msgRet.items[i], abs365CardConfig.item[i][1], abs365CardConfig.item[i][2])
    end

    -- 获得的道具
    local absAct = human.db.absAct[id]
    initDB(human, id)

    msgRet.getItems[0] = 0
    if absAct.item then
        msgRet.getItems[0] = #absAct.item
        for i = 1, #absAct.item do
            Grid.makeItem(msgRet.getItems[i], absAct.item[i][1], absAct.item[i][2])
        end
    end

    msgRet.newItem[0] = 0
    if absAct.newItem then
        Grid.makeItem(msgRet.newItem[1], absAct.newItem[1], absAct.newItem[2])
        msgRet.newItem[0] = 1
        absAct.newItem = nil
    end

    msgRet.leftDay = absAct and absAct.dayCnt or DAY_CNT
    msgRet.draw = absAct and absAct.draw or 0
    msgRet.active = absAct.active or 0

    msgRet.buyItem.item[0] = #buyItemConfig.reward
    for i = 1, #buyItemConfig.reward do
        Grid.makeItem(msgRet.buyItem.item[i], buyItemConfig.reward[i][1], buyItemConfig.reward[i][2])
    end

    msgRet.buyItem.cnt = 1
    msgRet.buyItem.id = TopupLogic.TOPUP_LIBAO_ID3
    msgRet.buyItem.maxCnt = buyItemConfig.cnt
    msgRet.buyItem.icon = buyItemConfig.icon or 0
    msgRet.buyItem.name = ""
    BuyLogic.fontBuyItem(human, msgRet.buyItem.buyMsg[1], buyItemConfig.buyID)
    msgRet.buyItem.buyMsg[0] = 1
    msgRet.buyItem.needItem[0] = 0
    Msg.send(msgRet, human.fd)
end

-- 领取道具
function get(human, id)
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

    local config = AbsActExcel.absActivity[id]
    if not config then return end

    local absAct = human.db.absAct[id]
    if not absAct then return end

    if not absAct.active then return end

    if #absAct.item <= 0 then return end
    
    BagLogic.addItemList(human, absAct.item, "abs_365Card")

    absAct.item = {}

    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, config.panelID)

    getAndSendMsg(human, id, config)
end


-- 积分购买
function abs365Card(human, id, buyConf, isFirst, cnt)
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

    local config = AbsActExcel.absActivity[id]
    if not config then return end

    local buyItemConfig = BuyExcel.libao[TopupLogic.TOPUP_LIBAO_ID3]
    if not buyItemConfig then return end

    AbsActLogic.checkAbsActClean(human, id)

    human.db.absAct[id].active = 1

    BagLogic.addItemList(human, buyItemConfig.reward, "abs_365Card")
end

-- 根据权重算索引
local function randomItemIndex(itemTable)
    local totalWeight = 0
    for i = 1, #itemTable do
        local item = itemTable[i]
        totalWeight = totalWeight + item[3]
    end

    if totalWeight <= 0 then
        return
    end

    local len = 0
    local index = nil
    local random = math.random(1, totalWeight)
    for i = 1, #itemTable do
        local item = itemTable[i]
        local weight = item[3]
        if random <= weight then
            index = i
            break
        else
            random = random - weight
        end
    end
    return index
end

function updateDaily(human, id)
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

    local config = AbsActExcel.absActivity[id]
    if not config then return end

    local actId = config.actId
    local abs365CardConfig = AbsActExcel.abs365Card[actId]
    if not abs365CardConfig then return end

    local absAct = human.db.absAct[id]
    if not absAct then return end

    if not absAct.dayCnt or absAct.dayCnt <= 0 then return end

    absAct.draw = 1
end

function draw(human, id)
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

    local config = AbsActExcel.absActivity[id]
    if not config then return end

    local actId = config.actId

    local abs365CardConfig = AbsActExcel.abs365Card[actId]
    if not abs365CardConfig then return end

    local absAct = human.db.absAct[id]
    if not absAct then return end

    if not absAct.dayCnt or absAct.dayCnt <= 0 then return end

    if not absAct.draw or absAct.draw ~= 1 then return end

    -- 随机道具1 奖池道具
    local index = randomItemIndex(abs365CardConfig.item)

    if not index or not abs365CardConfig.item[index] then return end

    local index2 = randomItemIndex(abs365CardConfig.item2)

    if not index2 or not abs365CardConfig.item2[index2] then return end

    absAct.dayCnt = absAct.dayCnt - 1
    absAct.draw = 0

    -- 加入到已有的道具中
    local itemReward = { [1] = { abs365CardConfig.item2[index2][1], abs365CardConfig.item2[index2][2] } }
    BagLogic.addItemList(human, itemReward, "abs_365Card")

    absAct.newItem = { abs365CardConfig.item[index][1], abs365CardConfig.item[index][2] }
    local bAdd = false
    for i = 1, #absAct.item do
        local item = absAct.item[i]
        if item[1] == abs365CardConfig.item[index][1] then
            item[2] = item[2] + abs365CardConfig.item[index][2]
            bAdd = true
            break
        end
    end

    if not bAdd then
        absAct.item[#absAct.item + 1] = { abs365CardConfig.item[index][1], abs365CardConfig.item[index][2] }
    end

    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, config.panelID)

    -- 刷新
    getAndSendMsg(human, id, config)
end